package com.icity.controller.screen;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.icity.common.Result;
import com.icity.entity.Echartpie;
import com.icity.entity.content.CatalogTable;
import com.icity.entity.system.TableSupport;
import com.icity.service.ContentStatisticalService;
import com.icity.service.ScreenService;
import com.icity.util.DateUtils;;

//内容统计
@RestController
@RequestMapping(value = "/admin/screen")
public class ScreenFenxiController {

	@Autowired
	private ScreenService screenService;

	@RequestMapping(value = "/pie")
	@ResponseBody
	public Result getCatalog(HttpServletRequest request) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<Object,Object> params = new HashMap<Object,Object>();
		String type = request.getParameter("type");
		String days = request.getParameter("days");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate) && StringUtils.isBlank(days)) {
			params.put("startDate", startDate);
			params.put("endDate", endDate);
		}else if(StringUtils.isBlank(days) || days.equals("7")){
			params.put("nowDate", sdf.format(new Date()));
			params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 6));
			params.put("endDate", sdf.format(new Date()));
		}else if(days.equals("14")){
			params.put("nowDate", sdf.format(new Date()));
			params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 13));
			params.put("endDate", sdf.format(new Date()));
		}else if(days.equals("30")){
			params.put("nowDate", sdf.format(new Date()));
			params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 29));
			params.put("endDate", sdf.format(new Date()));
		}
		params.put("type", type);
		Result result = new Result();
		try {
			Echartpie echartpie = screenService.getCatalogpie(params);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(echartpie);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

}
